//
//  HJVisitFriendViewCell.m
//  Keping
//
//  Created by a on 2017/8/29.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJVisitFriendViewCell.h"

@interface HJVisitFriendViewCell()<UIGestureRecognizerDelegate,UITextFieldDelegate>
///**
// *标记
// */
//@property(nonatomic,weak)UILabel*reuseIdLabel;
/**
 内容
 */
@property(nonatomic,weak)UILabel *contentLabel;
@property(nonatomic,weak)UITextField *contentField;
@property(nonatomic,weak)UIButton *contentBtn;
/**
 选择图片
 */
@property(nonatomic,weak)UIImageView*selectImgView;
@end
@implementation HJVisitFriendViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString*ID=@"HJVisitFriendViewCell";
    HJVisitFriendViewCell*cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell=[[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //添加子控件
        [self setAddSubView];
    }
    return self;
}
-(void)setAddSubView
{
    UILabel*contentLabel=[[UILabel alloc]init];
    [self.contentView addSubview:contentLabel];
    contentLabel.textColor=[UIColor colorWithHexString:@"#0e0e0e"];
    contentLabel.font = [UIFont systemFontOfSize:17];
    self.contentLabel=contentLabel;
    
    UIImageView*selectImgView=[[UIImageView alloc]init];
    selectImgView.image = [UIImage imageNamed:@"right_indicator"];
    [self.contentView addSubview:selectImgView];
    self.selectImgView=selectImgView;
    
    UITextField *field = [[UITextField alloc] init];
    field.textAlignment = NSTextAlignmentRight;
    field.delegate = self;
    [self.contentView addSubview:field];
    field.textColor=[UIColor colorWithHexString:@"#0e0e0e"];
    self.contentField = field;
    
    UIButton *button = [[UIButton alloc] init];
    [button setImage:[UIImage imageNamed:@"card_unlock"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"card_lock"] forState:UIControlStateSelected];
    [self.contentView addSubview:button];
    button.selected = NO;
    self.contentBtn = button;
}
-(void)setIsSelect:(BOOL)isSelect
{
    _isSelect=isSelect;
    self.selectImgView.highlighted=isSelect;
    self.contentBtn.selected = isSelect;
}
-(void)setTitle:(NSString *)title
{
    _title=title;
    self.contentLabel.text=title;
}

-(void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    self.contentField.enabled = isEdit;
}

- (void)setContent:(NSString *)content {
    _content = content;
    self.contentField.text = content;
}

- (void)setIsNomarl:(BOOL)isNomarl {
    _isNomarl = isNomarl;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat margin=K_FactorW(10);
    CGFloat imgWH=K_FactorW(11);
    CGFloat imgWW=K_FactorW(6);
    if (self.isNomarl){
        self.contentBtn.hidden = NO;
        self.selectImgView.hidden = YES;
    } else {
        self.contentBtn.hidden = YES;
        self.selectImgView.hidden = NO;
    }

    self.selectImgView.frame=(CGRect){{self.width-imgWW-margin,(self.height-imgWH)/2},{imgWW,imgWH}};
    self.contentBtn.frame = (CGRect){{self.width-15-margin,(self.height-15)/2},{15,15}};
    CGSize labelSize = [self sizeWithString:self.title];
    self.contentLabel.frame = (CGRect){{margin,0},{labelSize.width,self.height}};
    self.contentField.frame = (CGRect){{CGRectGetMaxX(self.contentLabel.frame) + 10,0},{self.width - CGRectGetMaxX(self.contentLabel.frame) - 30,self.height}};
}

- (CGSize)sizeWithString:(NSString *)text
{
    CGFloat margin=K_FactorW(10);
    CGSize frame = [text boundingRectWithSize:CGSizeMake(self.width - 2 * margin, self.height)
                                      options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}
                                      context:nil].size;
    return frame;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.visitFriend) {
        self.visitFriend(self, [textField.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "]);
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    /* 如果不是右对齐，直接返回YES，不做处理 */
    if (textField.textAlignment != NSTextAlignmentRight) {
        return YES;
    }
    
    /* 在右对齐的情况下*/
    // 如果string是@""，说明是删除字符（剪切删除操作），则直接返回YES，不做处理
    // 如果把这段删除，在删除字符时光标位置会出现错误
    if ([string isEqualToString:@""]) {
        return YES;
    }
    
    /* 在输入单个字符或者粘贴内容时做如下处理，已确定光标应该停留的正确位置，
     没有下段从字符中间插入或者粘贴光标位置会出错 */
    // 首先使用 non-breaking space 代替默认输入的@“ ”空格
    if ([string isEqualToString:@" "]) {
        string = [string stringByReplacingOccurrencesOfString:@" "
                                                   withString:@"\u00a0"];
        textField.text = [textField.text stringByReplacingCharactersInRange:range
                                                                 withString:string];
        //确定输入或者粘贴字符后光标位置
        UITextPosition *beginning = textField.beginningOfDocument;
        UITextPosition *cursorLoc = [textField positionFromPosition:beginning
                                                             offset:range.location+string.length];
        // 选中文本起使位置和结束为止设置同一位置
        UITextRange *textRange = [textField textRangeFromPosition:cursorLoc
                                                       toPosition:cursorLoc];
        // 选中字符范围（由于textRange范围的起始结束位置一样所以并没有选中字符）
        [textField setSelectedTextRange:textRange];
        return NO;
    }
    return YES;
}

@end
